<?php

/**
 * Elgg refer people action
 *
 * @package ReferPeople
 */

$site = elgg_get_site_entity();

//input for people to be refered
$emails = get_input('emails');
$first_name = get_input('first_name');
$last_name = get_input('last_name');
$telephone = get_input('telephone');
$emailmessage = get_input('comments');

//input for agent
//TODO agent email should be get from site settings.
//$agentemail = get_input('agentemail');
$agentemail = 'donggalvin@gmail.com';
$service = 'RRSP';

//validate the agent email
$agentemail = trim($agentemail);
if (strlen($agentemail) > 0) {
		$agentemail = preg_split('/\\s+/', $agentemail, -1, PREG_SPLIT_NO_EMPTY);
}

if (!is_array($agentemail) || count($agentemail) == 0) {
	register_error(elgg_echo('referpeople:noagentemails'));
	forward(REFERER);
}

//validate the information for people
$emails = trim($emails);
if (strlen($emails) > 0) {
	$emails = preg_split('/\\s+/', $emails, -1, PREG_SPLIT_NO_EMPTY);
}

$telephone = trim($telephone);

if ((!is_array($emails) || count($emails) == 0) && strlen($telephone) == 0 ) {
	register_error(elgg_echo('referpeople:nocontactinfo'));
	forward(REFERER);
}

$current_user = elgg_get_logged_in_user_entity();

$error = FALSE;
$warning = FALSE;
$bad_emails = array();
$notalready_members = array();
$sent_total = 0;
foreach ($agentemail as $email) {

	$email = trim($email);
	if (empty($email)) {
		continue;
	}

	// send out other email addresses
	if (!is_email_address($email)) {
		$error = TRUE;
		$bad_emails[] = $email;
		continue;
	}

//	if (!get_user_by_email($email)) {
//		$warning = TRUE;
//		$notalready_members[] = $email;

//		$link = elgg_get_site_url() . 'register?friend_guid=' . $current_user->guid . '&invitecode=' . generate_invite_code($current_user->username);
//	}

	$message = elgg_echo('referpeople:agentemail', array(
					$current_user->name,
					$service,
					$first_name,
					$last_name,
					$emails,
					$telephone
				)
	);
	
	if ($warning) {
		$message = $message . elgg_echo('referpeople:agentinvite', array(
		$site->name,
		$link)
		);
	}

	$subject = elgg_echo('referpeople:agentsubject', array($current_user->name, $first_name, $last_name));

	// create the from address
	$site = get_entity($site->guid);
	if ($site && $site->email) {
		$from = $site->email;
	} else {
		$from = 'noreply@' . get_site_domain($site->guid);
	}

	elgg_send_email($from, $email, $subject, $message);

	
	$sent_total++;
}

if ($warning) {
	register_error(elgg_echo('referpeople:agentnotuser'));
}

if ($error) {	
	register_error(elgg_echo('referpeople:invitations_sent', array($sent_total)));

	if (count($bad_emails) > 0) {
		register_error(elgg_echo('referpeople:email_error', array(implode(', ', $bad_emails))));
	}

	if (count($already_members) > 0) {
		register_error(elgg_echo('referpeople:already_members', array(implode(', ', $already_members))));
	}

} else {
	system_message(elgg_echo('referpeople:success' . $agentemail));
}

if ((!is_array($emails) || count($emails) == 0)) {
	forward(REFERER);
}

$error = FALSE;
$warning = FALSE;
$bad_emails = array();
$already_members = array();
$sent_total = 0;
foreach ($emails as $email) {

	$email = trim($email);
	if (empty($email)) {
		continue;
	}

	// send out other email addresses
	if (!is_email_address($email)) {
		$error = TRUE;
		$bad_emails[] = $email;
		continue;
	}

	if (!get_user_by_email($email)) {
		$warning = TRUE;
		$already_members[] = $email;
		$link = elgg_get_site_url() . 'register?friend_guid=' . $current_user->guid . '&invitecode=' . generate_invite_code($current_user->username);
	}

	$message = elgg_echo('referpeople:email', array(
					$current_user->name,
					$agentemail,
					$service
				)
	);
	
	if ($warning) {
		$message = $message . elgg_echo('referpeople:invite', array(
		$site->name,
		$link)
		);
	}

	$subject = elgg_echo('referpeople:subject', array($agentemail));

	// create the from address
	$site = get_entity($site->guid);
	if ($site && $site->email) {
		$from = $site->email;
	} else {
		$from = 'noreply@' . get_site_domain($site->guid);
	}

	elgg_send_email($from, $email, $subject, $message);
	$sent_total++;
}

if ($error) {	
	register_error(elgg_echo('referpeople:invitations_sent', array($sent_total)));

	if (count($bad_emails) > 0) {
		register_error(elgg_echo('referpeople:email_error', array(implode(', ', $bad_emails))));
	}

	if (count($already_members) > 0) {
		register_error(elgg_echo('referpeople:already_members', array(implode(', ', $already_members))));
	}

} else {
	system_message(elgg_echo('referpeople:success' . $emails));
}

forward(REFERER);
